package org.zjx.util.clickprecision;


import java.awt.*;

/**
 * 屏幕坐标计算器 - 根据窗口大小、人物坐标、地图坐标计算屏幕中心对应的像素坐标
 */
public class ScreenCoordinateCalculator {
    private final int screenWidth;
    private final int screenHeight;
    private final int pixelsPerUnit;
    private final int pixelMapWidth;
    private final int pixelMapHeight;
    private final Point screenCenter;

    public ScreenCoordinateCalculator(int screenWidth, int screenHeight, int pixelsPerUnit,
                                      int gameMapWidth, int gameMapHeight) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.pixelsPerUnit = pixelsPerUnit;
        this.pixelMapWidth = gameMapWidth * pixelsPerUnit;
        this.pixelMapHeight = gameMapHeight * pixelsPerUnit;
        this.screenCenter = new Point(screenWidth / 2, screenHeight / 2);
    }

    /**
     * 游戏坐标转像素地图坐标
     */
    public Point gameToPixelMap(Point gamePos) {
        return new Point(gamePos.x * pixelsPerUnit, gamePos.y * pixelsPerUnit);
    }

    /**
     * 计算屏幕中心在像素地图中的位置（考虑边界）
     */
    public Point calculateScreenCenterInPixelMap(Point playerGamePos) {
        Point playerPixel = gameToPixelMap(playerGamePos);

        int centerPixelX = calculateBoundedCenterX(playerPixel.x);
        int centerPixelY = calculateBoundedCenterY(playerPixel.y);

        return new Point(centerPixelX, centerPixelY);
    }

    /**
     * 根据屏幕点位计算对应的地图坐标（反向转换）
     */
    public Point screenToGamePosition(Point screenPos, Point screenCenterPixel) {
        int relativeX = screenPos.x - screenCenter.x;
        int relativeY = screenCenter.y - screenPos.y; // Y轴反向

        int gameX = screenCenterPixel.x + relativeX;
        int gameY = screenCenterPixel.y + relativeY;

        return new Point(gameX / pixelsPerUnit, gameY / pixelsPerUnit);
    }

    /**
     * 计算目标点在屏幕上的点击位置
     */
    public Point calculateScreenClickPosition(Point screenCenterPixel, Point targetGamePos) {
        Point targetPixel = gameToPixelMap(targetGamePos);

        Point relativeVector = new Point(
                targetPixel.x - screenCenterPixel.x,
                targetPixel.y - screenCenterPixel.y
        );

        int screenX = screenCenter.x + relativeVector.x;
        int screenY = screenCenter.y - relativeVector.y; // Y轴反向

        return new Point(screenX, screenY);
    }

    /**
     * 使用屏幕中心点直接计算点击位置（无边界限制）
     */
    public Point calculateScreenClickPositionByCenterPos(Point screenCenterPos, Point targetGamePos) {
        Point relativeVector = new Point(
                targetGamePos.x - screenCenterPos.x,
                targetGamePos.y - screenCenterPos.y
        );

        int screenX = screenCenter.x + relativeVector.x * pixelsPerUnit;
        int screenY = screenCenter.y - relativeVector.y * pixelsPerUnit;

        return new Point(screenX, screenY);
    }

    private int calculateBoundedCenterX(int playerPixelX) {
        if (playerPixelX < screenCenter.x) return screenCenter.x;
        return Math.min(playerPixelX, pixelMapWidth - screenCenter.x);
    }

    private int calculateBoundedCenterY(int playerPixelY) {
        if (playerPixelY < screenCenter.y) return screenCenter.y;
        return Math.min(playerPixelY, pixelMapHeight - screenCenter.y);
    }

    public Point getScreenCenter() {
        return new Point(screenCenter);
    }

    public boolean isScreenPositionValid(Point screenPos) {
        return screenPos.x >= 0 && screenPos.x <= screenWidth &&
                screenPos.y >= 0 && screenPos.y <= screenHeight;
    }
}